package com.m8n6.demo2.controller.before;

import cn.hutool.captcha.CaptchaUtil;
import com.m8n6.demo2.common.http.ResponseResult;
import com.m8n6.demo2.common.security.anno.AuthIgnore;
import com.m8n6.demo2.common.security.utils.ConfigrarionBean;
import com.m8n6.demo2.common.security.utils.RedisUtil;
import com.m8n6.demo2.entity.BUserEntity;
import com.m8n6.demo2.service.before.BUserService;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.keyvalue.core.CriteriaAccessor;
import org.springframework.web.bind.annotation.*;
import cn.hutool.captcha.CircleCaptcha;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/api/before")
public class BUserController {
    @Autowired
    private BUserService bUserService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ConfigrarionBean config;

    @AuthIgnore//TODO
    @PostMapping("/register")
    public ResponseResult<Map<String, String>> register(@RequestBody BUserEntity bUserEntity) {
        return bUserService.register(bUserEntity);
    }
    @AuthIgnore//TODO
    @PostMapping("/login")
    public ResponseResult<Map<String, String>> login(@RequestBody BUserEntity bUserEntity) {
        return bUserService.login(bUserEntity);
    }
    @AuthIgnore// TODO
    @GetMapping("/getcode")
    public void getcode(HttpServletResponse response)throws IOException {
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
        redisUtil.set("code", circleCaptcha.getCode(), config.getRedisExpiration());
        ServletOutputStream out = response.getOutputStream();
        circleCaptcha.write(out);
        out.close();
    }
}
